package com.usian.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class FollowBehaviorListener {

    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = {"followBehavior"})
    @Async
    public void test(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if (optional.isPresent()){
            String followBehavior =(String) record.value();
            FollowBehaviorDto followBehaviorDto = JSON.parseObject(followBehavior, FollowBehaviorDto.class);
            System.out.println("kafka自动审核======"+followBehaviorDto.toString());

            apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);

        }
    }
}
